package axp.demo.livewlp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.AssetManager;
import android.os.Environment;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.SurfaceHolder;
import java.io.FileOutputStream;
public class WallpaperMainService extends WallpaperService {
	public static final String PREFERENCES = "net.androgames.blog.sample.livewallpaper";
	public static final String PREFERENCE_VIDEO = "preference_video";
	
   // public static final String SHARED_PREFS_NAME_VIDEO = "videoname";
    public static final String folder = "video";
   	@Override
	public Engine onCreateEngine() {
		return new SampleEngine();
	}

	@Override
	public void onCreate() {
		super.onCreate();
		//transfer video to sdcard
	//	CopyAssets();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	public class SampleEngine extends Engine  implements SharedPreferences.OnSharedPreferenceChangeListener{
		private WallpaperPainterVideo painting;
		 private SharedPreferences prefs;

		SampleEngine() {
			VideoSurfaceHolder holder = new VideoSurfaceHolder(getSurfaceHolder());
			prefs = WallpaperMainService.this.getSharedPreferences(PREFERENCES, 0);
            prefs.registerOnSharedPreferenceChangeListener(this);
            onSharedPreferenceChanged(prefs, null);   
		//painting = new WallpaperPainterVideo(holder, getApplicationContext(),Integer.parseInt(prefs.getString(PREFERENCE_VIDEO, "1")));
			painting = new WallpaperPainterVideo(holder, getApplicationContext(),1);

		}

		public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
			
	//		String video = prefs.getString("preference_video", "1");
		//	VIDEO_SELECT = video;
		//	painting.mMediaPlayer.reset();
			//painting.mMediaPlayer.prepareAsync();	
			
	 //    painting.setVideo(Integer.parseInt(prefs.getString(PREFERENCE_VIDEO, "1")));
	       
		//	painting.setVideo(1);
		
		}

		@Override
		public void onCreate(SurfaceHolder surfaceHolder) {
			super.onCreate(surfaceHolder);
			setTouchEventsEnabled(true);
		//	CopyAssets();
		}

		@Override
		public void onDestroy() {
			super.onDestroy();
			// remove listeners and callbacks here
			painting.stopPainting();
		}

		@Override
		public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
			super.onSurfaceChanged(holder, format, width, height);
			// painting.setSurfaceSize(width, height);
		}

		@Override
		public void onSurfaceCreated(SurfaceHolder holder) {
			super.onSurfaceCreated(holder);
			painting.startPainting();
		}

		@Override
		public void onVisibilityChanged(boolean visible) {
			if (visible) {
				painting.resumePainting();
			} else {
				// remove listeners and callbacks here
				painting.pausePainting();
			}
		}

		@Override
		public void onSurfaceDestroyed(SurfaceHolder holder) {
			super.onSurfaceDestroyed(holder);
			boolean retry = true;
			painting.stopPainting();
		/*	while (retry) {
				try {
					painting.join();
					retry = false;
				} catch (InterruptedException e) {}
			}*/
		}
	}
}
